package com.yx.study.leet.primary.design;

import java.util.Stack;

/**
 * 最小栈 - 使用元组（当前值，当前最小值）
 * https://leetcode.cn/leetbook/read/top-interview-questions-easy/xnkq37/
 */
public class MinStack {
    // 数组栈, [当前值, 当前最小值]
    private Stack<int[]> stack = new Stack<>();

    public MinStack() {

    }

    public void push(int x) {
        if (stack.isEmpty()) {
            stack.push(new int[]{x, x});
        } else {
            stack.push(new int[]{x, Math.min(x, stack.peek()[1])});
        }
    }

    public void pop() {
        stack.pop();
    }

    public int top() {
        return stack.peek()[0];
    }

    public int getMin() {
        return stack.peek()[1];
    }
}
